package com.example.hydrogengenerator.bean

import android.text.TextUtils
import com.example.hydrogengenerator.base.App
import com.example.hydrogengenerator.event.AppViewModel
import com.example.hydrogengenerator.ext.gson
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.tencent.mmkv.MMKV
import java.util.*

object CacheUtil {

    fun getSettingBean(): SettingBean {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("SettingBean")
        return if (TextUtils.isEmpty(userStr)) {
            SettingBean().also { setSettingBean(it) }
        } else {
            Gson().fromJson(userStr, SettingBean::class.java)
        }
    }

    fun setSettingBean(userResponse: SettingBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("SettingBean", "")
        } else {
            kv.encode("SettingBean", gson.toJson(userResponse))
        }
    }

    fun geFactorySetting(): FactorySettingBean {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("FactorySettingBean")
        return if (TextUtils.isEmpty(userStr)) {
            FactorySettingBean().also { setFactorySetting(it) }
        } else {
            Gson().fromJson(userStr, FactorySettingBean::class.java)
        }
    }

    fun setFactorySetting(userResponse: FactorySettingBean?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("FactorySettingBean", "")
        } else {
            kv.encode("FactorySettingBean", gson.toJson(userResponse))
        }
    }

    val appViewModel: AppViewModel by lazy { App.instance.getAppViewModelProvider().get(AppViewModel::class.java) }


    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    /**
     * 设置是否已经登录
     */
    private fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    /**
     * 是否是第一次登陆
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次登陆
     */
    fun setFirst(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    /**
     * 获取搜索历史缓存数据
     */
    fun getSearchHistoryData(): ArrayList<String> {
        val kv = MMKV.mmkvWithID("cache")
        val searchCacheStr = kv.decodeString("history")
        if (!TextUtils.isEmpty(searchCacheStr)) {
            return gson.fromJson(
                searchCacheStr, object : TypeToken<ArrayList<String>>() {}.type
            )
        }
        return arrayListOf()
    }

    fun setSearchHistoryData(searchResponseStr: String) {
        val kv = MMKV.mmkvWithID("cache")
        kv.encode("history", searchResponseStr)
    }

    fun getUUID(): String {
        val kv = MMKV.mmkvWithID("app")
        var decodeString = kv.decodeString("uuid", "")
        if (decodeString.isEmpty()) {
            decodeString = UUID.randomUUID().toString()
            kv.encode("uuid", decodeString)
        }
        return decodeString
    }
}